//
//  SecondViewController.m
//  07-Delegate
//
//  Created by dllo on 16/7/22.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "SecondViewController.h"
#import "FirstViewController.h"

@interface SecondViewController ()
@property (nonatomic, strong) UITextField *textField;
@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 0, 200, 50)];
    _textField.text = _textString;
    _textField.backgroundColor = [UIColor colorWithRed:0.948 green:1.000 blue:0.812 alpha:1.000];
    [self.view addSubview:_textField];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(150, 200, 50, 50);
    [button setTitle:@"返回" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor cyanColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(backToLastVC:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

- (void)backToLastVC:(UIButton *)button {
    NSInteger count = self.navigationController.viewControllers.count;
    FirstViewController *firstVC = self.navigationController.viewControllers[count - 2];
    firstVC.titleTextField = _textField.text;
    // 代理人实现协议方法
#warning 触发协议方法 (判断delegate是否存在, 判断签订delegate的类是否实现了协议方法)
    if (self.delegate && [self.delegate respondsToSelector:@selector(passValue:)]) {
        [self.delegate passValue:_textField.text];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
